package com.spider.web.url;

import java.net.URL;
import java.util.*;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.beans.LinkBean;
import org.htmlparser.beans.StringBean;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

 
import com.spider.web.utils. Charset;
import com.spider.web.vo.Links;
 
 

/**
 * 取到传入URL页面上的所有的超级连接。
 * 
 * @author pc221
 * 
 */
public class GetLinksFromOnePage {

	private Links linkset = new Links();

	private Set<String> tmpurl = new HashSet<String>();

	private Parser parser;

	private URL[] url = new URL[] {};

	private static int count = 1;

	private GetLinksFromOnePage(String url) {
		initURLS(url);
	}

	// private GetLinksFromOnePage(String url,String start,String end) {
	// initURLS(url,start,end);
	// }
	private GetLinksFromOnePage(String url, String filterurl, String charset) {
		linkset = filter(url, filterurl, charset);
	}

	public static GetLinksFromOnePage getInstance(String baseUrl) {
		GetLinksFromOnePage linkspage = new GetLinksFromOnePage(baseUrl);
		return linkspage;
	}

	public static GetLinksFromOnePage getInstance(String seed, String father,
			String charset) {
		GetLinksFromOnePage linkspage = new GetLinksFromOnePage(seed, father,
				charset);
		return linkspage;
	}

	public static GetLinksFromOnePage getInstance(String seed, String father) {
		GetLinksFromOnePage linkspage = new GetLinksFromOnePage(seed, father,
				"");
		return linkspage;
	}

	// public static GetLinksFromOnePage getInstance(String baseUrl,String
	// start,String end) {
	// GetLinksFromOnePage linkspage = new GetLinksFromOnePage(baseUrl,start
	// ,end);
	// return linkspage;
	// }

	/**
	 * HtmlParser的linkBean取
	 * 
	 * @param baseUrl
	 */
	private void initURLS(String baseUrl) {
		LinkBean bean = new LinkBean();
		bean.setURL(baseUrl);
		url = bean.getLinks();
		System.out.println(url);
	}

	/**
	 * 使用过滤器 榨取符合条件的 LinkText 和Link href
	 * @param url  是解析的url
	 * @param filterUrl  过滤的字符串。 
	 * @param _enCode
	 * @return
	 */
	private Links filter(String url, String urlPart, String _enCode) {
		Links link = new Links();
		if (null == urlPart
				|| null == url||"".equals(urlPart.trim()) || "".equals(url.trim()) )
			return link;
		try {
			if (null == parser)
				parser = new Parser(url);
			if (url.equals(parser.getURL()))
				parser.reset();
			parser.setEncoding((_enCode == null || "".equals(_enCode)) ?  Charset.UTF_8:_enCode);
			NodeFilter filter = new LinkStringFilter(urlPart);
			NodeList list = parser.extractAllNodesThatMatch(filter);//匹配url. url和filter匹配，返回匹配的。
			if (list != null) {
				for (int x = 0; x < list.size(); x++) {
					Node tag = list.elementAt(x);
					if (tag instanceof LinkTag) {
						LinkTag obj = (LinkTag) tag;
						String objurl = obj.extractLink();
						String objtext = obj.getLinkText();
						{
							if (objurl.contains("#"))
								continue;
							if (objurl.startsWith("/")){
								objurl = url + objurl;
							}
//							if(objurl.endsWith("/"))
//								objurl.substring(objurl.length()-1, objurl.length());
							if ("".equals(objurl) && "".equals(objtext))
								continue;
						}
						System.out.println(count++ + "：临时---【" + objurl + "】"
								+ "TXT【" + objtext + "】");
						link.addLink(objtext, objurl);
						tmpurl.add(objurl);
					}
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("萃取Url信息出错");
		}

		return link;

	}

	public static void main(String[] arg) {
		GetLinksFromOnePage page = GetLinksFromOnePage.getInstance(
				"http://www.yingjiesheng.com/shenzhen", "24333");
		page.filter("http://www.yingjiesheng.com/shenzhen", "sheng.com/job_", "gbk");
		// GetLinksFromOnePage link2 = GetLinksFromOnePage.getInstance(
		// "http://127.0.0.1:8081/spider20/spider/test.html", "/sina/");
		// Links links = link.getLinkset();
		//
		// for (int x = 0; x < links.size(); x++) {
		// System.out.println(links.getText(x));
		// }

	}

	/**
	 * 匹配URL开头 如果Url_Start_Str=null或者“” 就不过滤
	 * 
	 * @param Url_Start_Str
	 * @return
	 */
	// public Set getUrlByUrlStartsWith(String Url_Start_Str) {
	// Set<String> set = new HashSet<String>();
	//
	// for (Object link : url) {
	//
	// if (link != null) {
	// if (StringUtils.isBlank(Url_Start_Str)
	// && link.toString().startsWith(Url_Start_Str)) {
	// System.out.println("合作企业全职招聘职位--->>>" + link);
	// set.add(link.toString());
	// }
	// }
	// }
	// return set;
	// }
	/**
	 * 后缀匹配
	 * 
	 * @param Url_End_Str
	 * @return
	 */
	// public Set getUrl_By_UrlEndsWith(String Url_End_Str) {
	// Set<String> set = new HashSet<String>();
	// for (Object link : url) {
	// if (link != null) {
	// if (StringUtils.isBlank(Url_End_Str)
	// && link.toString().endsWith(Url_End_Str)) {
	// System.out.println("合作企业全职招聘职位--->>>" + link);
	// set.add(link.toString());
	// }
	// }
	// }
	// return set;
	// }
	/**
	 * 前缀AND后缀 同时匹配
	 * 
	 * @param Url_Start_Str
	 * @param Url_End_Str
	 * @return
	 */
	public Set getUrlByUrlStartsWith_And_UrlEndsWith(String Url_Start_Str,
			String Url_End_Str) {
		Set<String> list = new HashSet<String>();
		for (Object link : url) {
			if (link != null) {
				if (link.toString().startsWith(Url_Start_Str)
						&& link.toString().endsWith(Url_End_Str)) {
					System.out.println("合作企业全职招聘职位--->>>" + link);
					list.add(link.toString());
				}
			}
		}
		return list;
	}

	/**
	 * url 中部分匹配http://my.yingjiesheng.com/index.php/display/jobs/20287.htm
	 * 
	 * @param match_point
	 * @return
	 */
	public Set getUrlByUrl_Matchs(String match_point) {
		Set<String> list = new HashSet<String>();
		for (Object link : url) {
			if (link != null) {
				// System.out.println("合作企业全职招聘职"+link.toString()+"|||||||||||||"+match_point);
				if (link.toString().contains(match_point)) { // 以后要更换称match
					// 使用正则表达式进行匹配
					if (link.toString().contains("#"))
						continue;
					System.out.println("生成URL并过滤 --->>>" + link);
					list.add(link.toString());
				}
			}
		}
		return list;
	}

	public Links getLinkset() {
		return linkset;
	}

	public void setLinkset(Links linkset) {
		this.linkset = linkset;
	}

	public Set<String> getTmpurl() {
		return tmpurl;
	}

	public void setTmpurl(Set<String> tmpurl) {
		this.tmpurl = tmpurl;
	}

}
